import * as Cesium from 'cesium/Cesium'
import Measurement from "./Measurement";
import MeasurementSettings from "./MeasurementSettings";
import MeasureUnits from "./MeasureUnits";
import PolylinePrimitive from "./PolylinePrimitive";
import getWorldPosition from "./getWorldPosition";
import getHeightPosition from "./getHeightPosition";


var Mode = {
    BeforeDraw: 0,
    Drawing: 1,
    AfterDraw: 2
}
    , scratch$2 = new Cesium.Cartesian3
    , positionScratch$f = new Cesium.Cartesian3;

function getIcon(e) {
    return '<svg viewBox="0 0 30 30" height="' + e + 'px" width="' + e + 'px">\n                 <g transform="translate(0,-267)">\n                   <path d="m 15.042838,272.34414 -0.0497,18.93758"/>\n                   <circle r="2.0788691" cy="270.01154" cx="15.078616"/>\n                   <circle r="2.0788691" cy="293.97095" cx="15.092237"/>\n                 </g>\n               </svg>'
}

function VerticalMeasurement(e) {
    e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT),
        Measurement.call(this, e);
    var t = this._pointCollection
        , i = [new Cesium.Cartesian3, new Cesium.Cartesian3];
    this._startPoint = t.add(MeasurementSettings.getPointOptions()),
        this._endPoint = t.add(MeasurementSettings.getPointOptions()),
        this._positions = i,
        this._polyline = this._primitives.add(new PolylinePrimitive(MeasurementSettings.getPolylineOptions({
            ellipsoid: this._scene.frameState.mapProjection.ellipsoid,
            positions: i
        }))),
        this._label = this._labelCollection.add(MeasurementSettings.getLabelOptions({
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            verticalOrigin: Cesium.VerticalOrigin.TOP,
            pixelOffset: new Cesium.Cartesian2(10,10)
        })),
        this._mode = Mode.BeforeDraw,
        this._draggingPlane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X,0),
        this._surfaceNormal = new Cesium.Cartesian3,
        this._distance = 0
}
VerticalMeasurement.prototype = Object.create(Measurement.prototype),
    VerticalMeasurement.prototype.constructor = VerticalMeasurement,
    Cesium.defineProperties(VerticalMeasurement.prototype, {
        distance: {
            get: function() {
                return this._distance
            }
        },
        type: {
            value: "垂直距离"
        },
        icon: {
            value: getIcon(15)
        },
        thumbnail: {
            value: getIcon(25)
        },
        instructions: {
            value: ["单击点云或地球仪以设置起点","移动鼠标以拖动直线","再次单击以设置终点","若要进行新测量，请单击以清除以前的测量"]
        },
        id: {
            value: "verticalMeasurement"
        }
    }),
    VerticalMeasurement.prototype.handleClick = function(e) {
        var t = this._scene
            , i = t.frameState.mapProjection.ellipsoid;
        this._mode === Mode.AfterDraw && this.reset();
        var r = this._mode
            , n = this._positions;
        if (r === Mode.BeforeDraw) {
            var a = VerticalMeasurement._getWorldPosition(t, e, n[0]);
            if (!Cesium.defined(a))
                return;
            this._polyline.show = !0,
                n[0] = Cesium.Cartesian3.clone(a, n[0]),
                n[1] = Cesium.Cartesian3.clone(a, n[1]),
                this._startPoint.position = a,
                this._startPoint.show = !0,
                this._mode = Mode.Drawing,
                this._polyline.positions = n,
                this._surfaceNormal = i.geodeticSurfaceNormal(a, this._surfaceNormal)
        } else
            r === Mode.Drawing && (this._endPoint.position = n[1],
                this._endPoint.show = !0,
                this._mode = Mode.AfterDraw)
    }
    ,
    VerticalMeasurement.prototype.handleMouseMove = function(e) {
        if (this._mode === Mode.Drawing) {
            var t = this._label;
            if (this._scene.mode === Cesium.SceneMode.SCENE2D)
                return t.position = this._positions[0],
                    t.text = MeasureUnits.distanceToString(0, this._selectedUnits.distanceUnits, this._selectedLocale),
                    t.show = !0,
                    void (this._mode = Mode.AfterDraw);
            if (Cesium.defined(VerticalMeasurement._getHeightPosition(this, e))) {
                var i = this._positions
                    , r = i[0]
                    , n = i[1]
                    , a = Cesium.Cartesian3.subtract(n, r, scratch$2)
                    , o = Cesium.Cartesian3.magnitude(a);
                t.position = Cesium.Cartesian3.midpoint(r, n, positionScratch$f),
                    t.text = MeasureUnits.distanceToString(o, this._selectedUnits.distanceUnits, this._selectedLocale),
                    t.show = !0,
                    this._polyline.positions = i,
                    this._distance = o
            }
        }
    }
    ,
    VerticalMeasurement.prototype.reset = function() {
        this._polyline.show = !1,
            this._label.show = !1,
            this._startPoint.show = !1,
            this._endPoint.show = !1,
            this._mode = Mode.BeforeDraw,
            this._distance = 0
    }
    ,
    VerticalMeasurement.prototype.isDestroyed = function() {
        return !1
    }
    ,
    VerticalMeasurement.prototype.destroy = function() {
        this._primitives.remove(this._polyline);
        var e = this._pointCollection;
        return e.remove(this._startPoint),
            e.remove(this._endPoint),
            this._labelCollection.remove(this._label),
            Cesium.destroyObject(this)
    }
    ,
    VerticalMeasurement._getWorldPosition = getWorldPosition,
    VerticalMeasurement._getHeightPosition = getHeightPosition;
export default VerticalMeasurement;